package com.springBoot.controller;

import com.springBoot.commonClass.JwtUtils;
import com.springBoot.commonClass.PassToken;
import com.springBoot.entity.UserEntity;
import com.springBoot.entity.union.UserWithRoleEntity;
import com.springBoot.exception.BusinessFailureException;
import com.springBoot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.naming.AuthenticationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/user-operation")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/query-user-list")
    public List<Map<String, String>> retrieveByParams(UserEntity entity){
      if(entity == null){
          entity = new UserEntity();
      }
      return userService.retrieveByParams(entity);
    };

    @GetMapping("/query-user-info")
    public Map<String, String> getUserInfoByUserId(String userId){
        UserWithRoleEntity userEntity = null;
        userEntity = userService.getUserInfoByUserId(userId);

        Map<String, String> response = new HashMap<>();

        String Id = userEntity.getUserId();
        String userName = userEntity.getUserName();
        String orgId = userEntity.getOrgId();
        String roleName = userEntity.getRoleName();
        String userAcc = userEntity.getUserAcc();
        String userPwd = userEntity.getUserPwd();

        response.put("userId", Id);
        response.put("userAcc", userAcc);
        response.put("userName", userName);
        response.put("userPwd", userPwd);
        response.put("roleName", roleName);

        return response;
    };

    @GetMapping("/login")
    @PassToken
    public Map<String, String> login(String userAcc, String userPwd) throws Exception {
        UserWithRoleEntity userEntity = null;
        try{
            userEntity = userService.login(userAcc, userPwd);
        }catch (AuthenticationException e){
            throw new BusinessFailureException(true);
        }

        //如果成功了，聚合需要返回的信息
        Map<String, String> response = new HashMap<>();
        //分配一个token
        String jwtToken = JwtUtils.createToken(userEntity.getUserId(),userAcc, userEntity.getUserName());
        String userId = userEntity.getUserId();
        String userName = userEntity.getUserName();
        String orgId = userEntity.getOrgId();
        String roleName = userEntity.getRoleName();
        response.put("userId", userId);
        response.put("userAcc", userAcc);
        response.put("userName", userName);
        response.put("token", jwtToken);
        response.put("orgId", orgId);
        response.put("roleName", roleName);

        return response;
    }

    @PostMapping("/update-user-info")
    public UserWithRoleEntity updateUserInfo(@RequestBody UserWithRoleEntity userWithRoleEntity){
        boolean isSuccess = userService.updateUserInfo(userWithRoleEntity);
//        return userWithRoleEntity;

        if (isSuccess){
            return userWithRoleEntity;
        }
        return null;
    };

    @PostMapping("/insert-user-info")
    public UserWithRoleEntity addNewUserInfo(@RequestBody UserWithRoleEntity userWithRoleEntity) throws ExecutionException, InterruptedException {

        UserWithRoleEntity res =  userService.insertNewUserInfo(userWithRoleEntity).get();
        return res;
    }

    @PostMapping("delete-user")
    public void deleteUser(@RequestBody Map<String,String> body){
        String userId = body.get("userId");
        String lastModifiedBy = body.get("lastModifiedBy");
        userService.deleteUser(userId, lastModifiedBy);
    }

}
